<template>
  <div class="login">
    <div class="left"><img src="@/assets/img/login.png" alt="" /></div>
    <div class="right">
      <h3>后台管理</h3>
      <el-form
        :model="form"
        status-icon
        :rules="rules"
        ref="form"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            type="username"
            prefix-icon="el-icon-user"
            v-model.trim="form.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="el-icon-lock"
            v-model.trim="form.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">提交</el-button>
          <el-button @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { UserInfo } from "@/types/index";

// 声明在外部
var checkAge = (rule: any, value: String, callback: Function) => {
  if (!value) {
    return callback(new Error("账号不能为空"));
  }
  setTimeout(() => {
    let reg = /[\u4e00-\u9fa5_a-zA-Z0-9_]{4,10}/;
    // 使用断言 <string> 或者 value as string
    if (reg.test(<string>value)) {
      callback();
    } else {
      callback(new Error("格式有误"));
    }
  }, 1000);
};

@Component
export default class Login extends Vue {
  // 声明在类里面使用this.checkAge
  // public checkAge = (rule: any, value: String, callback: Function) => {
  //   if (!value) {
  //     return callback(new Error("账号不能为空"));
  //   }
  //   setTimeout(() => {
  //     let reg = /[\u4e00-\u9fa5_a-zA-Z0-9_]{4,10}/;
  //     if (reg.test(<string>value)) {
  //       callback();
  //     } else {
  //       callback(new Error("格式有误"));
  //     }
  //   }, 1000);
  // };

  public form: UserInfo = {
    username: "admin",
    password: "123456",
  };

  rules = {
    username: [{ validator: checkAge, trigger: "blur" }],
    password: [
      { required: true, message: "请输入密码", trigger: "blur" },
      { min: 6, max: 20, message: "格式不正确", trigger: "blur" },
    ],
  };

  public submitForm(formName: any) {
    const elForm: any = this.$refs[formName];
    elForm.validate(async (valid: boolean) => {
      let res = {
        msg: "登录成功",
      };
      if (valid) {
        this.$router.push("/layout");
        this.$message.success(res.msg);
      } else {
        this.$message.error(res.msg);
        return false;
      }
    });
  }

  public resetForm(formName: any) {
    const elForm: any = this.$refs[formName];
    elForm.resetFields();
  }
}
</script>

<style lang="scss" scoped>
@import "@/assets/css/variables.scss";

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: $bg;
}

.left {
  width: 400px;
  height: 300px;
  animation: move 5s linear infinite;

  img {
    width: 100%;
    height: 100%;
    transform: rotateY(180deg);
  }
}

.right {
  padding: 0 20px 0;
  width: 300px;
  height: 300px;
  border-radius: 30px;
  border: 1px solid $font-color;
  text-align: center;

  .el-form {
    margin-top: 50px;
  }

  h3 {
    text-align: center;
    font-size: 33px;
    color: $font-color;
  }
}

@keyframes move {
  0% {
    -webkit-transform: translateY(0px);
    transform: translateY(0px);
  }
  50% {
    -webkit-transform: translateY(20px);
    transform: translateY(20px);
  }
  10% {
    -webkit-transform: translateY(0px);
    transform: translateY(0px);
  }
}
</style>
